# coding: utf-8
import json
from pprint import pprint
from math import floor
from specialEquipLists import *
from attrUtils import 会心或破防百分比计算数值, 会效百分比计算数值, 基于面板数值计算百分比


# 将魔盒的json串中的key进行中文映射
def 方法_解析魔盒Json(parsedJson: dict) -> dict:
    result = {}
    for key, value in parsedJson.items():
        if attribute2cnDict.get(key, "none") != "none":
            result[attribute2cnDict[key]] = value
        else:
            result[key] = value
    return result


# 输出常态裸BUFF角色面板，用于用户检查
def 方法_输出角色面板(带百分比面板: dict):
    不输出列表 = ["最小武器伤害", "最大武器伤害", "外功破防数值", "外功破防", "外功最终攻击"]
    百分比列表 = ["外功会心", "外功会效", "毒性内功破防", "无双"]
    for 字段名, 字段值 in 带百分比面板.items():
        if 字段名 in 不输出列表:
            continue
        elif 字段名 in 百分比列表:
            if 字段名 == "外功会心":
                print("  %s : %s%% （已带机关/弩箭额外增加的1%%会心，若不符请检查自身buff[12740]）" % (字段名, str(round(字段值 * 100, 2))))
                continue
            print("  %s : %s%%" % (字段名, str(round(字段值 * 100, 2))))
            continue
        print("  %s : %s" % (字段名, floor(字段值)))
    print()


# 分析装备可能携带的buff类增益（魔盒配装器已将数值类增益直接加到面板上，如切糕、衣帽大附魔等；而水/风特效、套装技能增伤等，需要手动分析得出）
def 方法_分析装备携带增益(装备列表: dict) -> dict:
    # 其中，key为增益名，value为增益详情（如数值、具体BUFF等）
    result = {"套装": []}
    # 初始化套装装备情况
    套装穿戴计数 = {}
    for 套装名 in 套装列表.keys():
        套装穿戴计数[套装名] = 0

    # 开始分析
    for 部位, 装备详情字典 in 装备列表.items():
        # 如果某个部位无装备，则装备列表中不会有该位置的键值对，故只要辨识到key，则一定可以分析。
        if 部位 == "PRIMARY_WEAPON":
            # 武器
            武器ID = 装备详情字典["id"]
            if 武器ID in 水特效武器ID列表.keys():
                result["水特效"] = 水特效武器ID列表[武器ID]["数值"]
            continue
        elif 部位 == "PENDANT":
            # 腰坠
            腰坠ID = 装备详情字典["id"]
            if 腰坠ID in 风特效腰坠ID列表.keys():
                result["风特效"] = 风特效腰坠ID列表[腰坠ID]["数值"]
            continue
        elif 部位 == "HAT" or 部位 == "JACKET" or 部位 == "BELT" or 部位 == "WRIST" or 部位 == "SHOES":
            # 套装穿戴计数
            for 套装名, 套装详情字典 in 套装列表.items():
                装备ID = 装备详情字典["id"]
                if 装备ID in 套装详情字典["套装装备ID列表"]:
                    套装穿戴计数[套装名] = 套装穿戴计数[套装名] + 1
            # 帽子大附魔
            if 部位 == "HAT":
                帽子大附魔ID = str(装备详情字典.get("enchant", "none"))
                if 帽子大附魔ID in 帽子大附魔列表.keys():
                    result["帽子大附魔"] = 帽子大附魔列表[帽子大附魔ID]
            # 衣服大附魔
            elif 部位 == "JACKET":
                衣服大附魔ID = str(装备详情字典.get("enchant", "none"))
                if 衣服大附魔ID in 衣服大附魔列表.keys():
                    result["衣服大附魔"] = 衣服大附魔列表[衣服大附魔ID]
            elif 部位 == "BELT":
                腰带大附魔ID = str(装备详情字典.get("enchant", "none"))
                if 腰带大附魔ID in 腰带护腕鞋子大附魔列表.keys():
                    result["腰带大附魔"] = 腰带护腕鞋子大附魔列表[腰带大附魔ID]
            elif 部位 == "WRIST":
                护腕大附魔ID = str(装备详情字典.get("enchant", "none"))
                if 护腕大附魔ID in 腰带护腕鞋子大附魔列表.keys():
                    result["护腕大附魔"] = 腰带护腕鞋子大附魔列表[护腕大附魔ID]
            elif 部位 == "SHOES":
                鞋子大附魔ID = str(装备详情字典.get("enchant", "none"))
                if 鞋子大附魔ID in 腰带护腕鞋子大附魔列表.keys():
                    result["鞋子大附魔"] = 腰带护腕鞋子大附魔列表[鞋子大附魔ID]



    for 套装名, 套装穿戴数量 in 套装穿戴计数.items():
        if 套装穿戴数量 >= 2:
            result["套装"].append(套装列表[套装名]["2件套效果"])
        if 套装穿戴数量 >= 4:
            result["套装"].append(套装列表[套装名]["4件套效果"])

    # pprint(装备列表)
    # pprint(result)
    return result


def 方法_基于魔盒配装器Json分析角色面板及装备增益情况(jsonInput: str):
    if len(jsonInput) < 0:
        return None
    try:
        # 尝试将用户输入的JSON字符串解析为字典
        parsedJson = 方法_解析魔盒Json(json.loads(jsonInput))
        # print(parsedJson)

        # 配装列表用来解析套装、武器、腰坠以及衣服、头的大附魔
        装备列表 = parsedJson["EquipList"]
        装备携带增益列表 = 方法_分析装备携带增益(装备列表)
        # 奇穴未来用于解析分星/千机、杀机/雷甲等，目前用不到
        奇穴列表 = parsedJson["TalentCode"]
        # 基于魔盒配装器Json数据构造角色面板属性，其中外功破防与外功最终攻击需要单独计算衣/帽大附魔
        面板属性 = {
            "元气": parsedJson["元气"],
            "基础攻击": parsedJson["基础毒性内功攻击"],
            "最终攻击": parsedJson["最终毒性内功攻击"],
            "外功会心数值": 会心或破防百分比计算数值(parsedJson["外功会心率"] - 0.01),  # 魔盒给唐门自带了1%的机关/弩箭会心，此处避免重复计算
            "外功会效数值": 会效百分比计算数值(parsedJson["外功会效"]),
            "毒性内功破防数值": parsedJson["毒性内功破防等级"],      # 此为基础破防数值而非面板破防数值
            "最终毒性内功破防数值": parsedJson["毒性内功破防等级"],
            "无双数值": parsedJson["无双等级"],
            "破招": parsedJson["破招"],
            "最小武器伤害": parsedJson["武器伤害（基础值）"],
            "最大武器伤害": parsedJson["武器伤害（基础值）"] + parsedJson["武器伤害（随机值）"],
            "外功破防数值": 12 + 装备携带增益列表.get("帽子大附魔", 0),  # 120级裸面板属性 + 大附魔数值（没有就是0）
            "最终外功破防数值": 12 + 装备携带增益列表.get("帽子大附魔", 0),
            "外功基础攻击": 6 + 装备携带增益列表.get("衣服大附魔", 0),
            "外功最终攻击": 6 + 装备携带增益列表.get("衣服大附魔", 0),   # 120级裸面板属性 + 大附魔数值（没有就是0）
            "加速等级": parsedJson["加速等级"]
        }
        带百分比面板 = 基于面板数值计算百分比(面板属性)
        return 带百分比面板, 装备携带增益列表
    except json.JSONDecodeError as e:
        print(f"JSON解析错误：{e}")
    return None, None


# 方法_基于魔盒配装器Json分析角色面板及装备增益情况('{"Vitality":32388,"Agility":41,"Spirit":41,"Spunk":6634,"Strength":41,"PoisonAttackPowerBase":36211,"PoisonAttackPower":47820,"PhysicsCriticalStrikeRate":0.3882759388215842,"PhysicsCriticalDamagePowerPercent":1.9361978101858162,"PoisonOvercomePercent":0.4660497949060384,"StrainPercent":0.4162684632811959,"HastePercent":0.009151800173604363,"SurplusValue":4561,"MaxHealth":462491,"PhysicsShieldPercent":0.06544026434217626,"PoisonShieldPercent":0.09589770911028236,"ToughnessDefCriticalPercent":0,"DecriticalDamagePercent":0.13737211465291282,"MeleeWeaponAttackSpeed":16,"MeleeWeaponDamage":2163,"MeleeWeaponDamageRand":1442,"PoisonOvercome":36642,"Strain":31557,"Haste":883,"EquipList":{"HAT":{"id":"7_96208","stone":"","enchant":12037,"enhance":11888,"strength":6,"embedding":[8,8]},"BELT":{"id":"7_96148","stone":"","enchant":12033,"enhance":12004,"strength":6,"embedding":[8,8]},"SHOES":{"id":"7_96178","stone":"","enchant":12034,"enhance":11943,"strength":6,"embedding":[8,8]},"WRIST":{"id":"7_96338","stone":"","enchant":12035,"enhance":11940,"strength":3,"embedding":[8,8]},"JACKET":{"id":"7_96238","stone":"","enchant":12036,"enhance":12003,"strength":6,"embedding":[8,8]},"RING_1":{"id":"8_38795","stone":"","enchant":"","enhance":12014,"strength":3,"embedding":[]},"RING_2":{"id":"8_38800","stone":"","enchant":"","enhance":12014,"strength":3,"embedding":[]},"BOTTOMS":{"id":"7_96350","stone":"","enchant":"","enhance":11879,"strength":3,"embedding":[8,8]},"PENDANT":{"id":"8_38789","stone":"","enchant":"","enhance":12008,"strength":4,"embedding":[8]},"NECKLACE":{"id":"8_38753","stone":"","enchant":"","enhance":12005,"strength":6,"embedding":[8]},"PRIMARY_WEAPON":{"id":"6_35763","stone":4886,"enchant":"","enhance":57,"strength":4,"embedding":[8,8,8]},"SECONDARY_WEAPON":{"id":"6_34701","stone":"","enchant":"","enhance":12022,"strength":6,"embedding":[8]}},"Title":"测试","TalentCode":[{"id":28371,"name":"血影留痕"},{"id":6493,"name":"天风汲雨"},{"id":6495,"name":"弩击急骤"},{"id":6506,"name":"千机之威"},{"id":6441,"name":"流星赶月"},{"id":6451,"name":"聚精凝神"},{"id":18249,"name":"化血迷心"},{"id":14857,"name":"雷甲三铉"},{"id":6461,"name":"秋风散影"},{"id":5715,"name":"回肠荡气"},{"id":14856,"name":"曙色催寒"},{"id":18675,"name":"千秋万劫"}],"Mount":10225}')